package com.gp.testonline.controller;

import com.gp.testonline.eo.ExamquestionEOPage;
import com.gp.testonline.model.Examquestion;
import com.gp.testonline.service.ExamquestionService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "examquestion")
@RestController
public class ExamquestionController {
    @Autowired
    private ExamquestionService examquestionService;

    @ApiOperation(value = "试题信息添加方法")
    @RequestMapping(value = "/insertExamquestion",method = RequestMethod.GET)
    public ResponseMessage insert(Examquestion examquestion){
        int IsInsert=0;
        //传入数据的判断
        //(1) examquestion不能为空
        if (examquestion!=null){
            //(2) 试题编号不能为0
            if (examquestion.getEqnumber()!=0){
                //(3) 添加数据
                IsInsert=examquestionService.InsertExamquestion(examquestion);
                if (IsInsert==1){
                    return Result.success("添加成功！");
                }else if (IsInsert==-1){
                    return Result.error("试题编号重复不允许添加！");
                }else{
                    return Result.error("保存失败！");
                }
            }
        }
        return Result.error("保存失败！");
    }

    @ApiOperation(value = "试题信息修改方法")
    @RequestMapping(value ="/updateExamquestion",method = RequestMethod.GET)
    public ResponseMessage updateExamquestion(Examquestion examquestion){
        int isUpdate=0;
        //传入的参数不能为空
        if (examquestion!=null){
            //参数当中主键不能为0
            if (examquestion.getEqid()!=0){
                isUpdate=examquestionService.UpdateExamquestion(examquestion);
                if (isUpdate==1){
                    return Result.success("更新成功！");
                }else {
                    return Result.error("更新失败！试题编号重复！");
                }
            }else {
                return Result.error("传入的参数不正确！");
            }
        }
        return Result.error("传入的参数不正确！");
    }

    @ApiOperation(value = "试题信息删除方法")
    @RequestMapping(value = "/deleteExamquestion",method = RequestMethod.GET)
    public ResponseMessage deleteExamquestion(int eqid){
        int isDelete=0;
        if (eqid!=0){
            isDelete=examquestionService.DeleteExamquestion(eqid);
            if (isDelete>0){
                return Result.success("删除成功！");
            }else{
                return Result.error("删除失败！");
            }
        }else {
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "根据条件获取试卷考题信息")
    @RequestMapping(value = "/getExamquestion",produces = "application/json;charest=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getExamquestion(ExamquestionEOPage examquestionEOPage) {
        Map<String, List> map = new HashMap<>();
        List<ExamquestionEOPage> listExamquestion = examquestionService.getExamquestionAll(examquestionEOPage);
        map.put("list",listExamquestion);
        return Result.success("200",map);
    }
}
